<template>
  <el-form ref="form" :model="user" :rules="rules">
    <el-form-item :label="$t('旧密码')" prop="oldPassword">
      <el-input v-model="user.oldPassword" :placeholder="$t('旧密码')" type="password" show-password auto-complete="new-password" />
    </el-form-item>
    <el-form-item :label="$t('新密码')" prop="newPassword">
      <el-input v-model="user.newPassword" :placeholder="$t('新密码')" type="password" show-password auto-complete="new-password" />
    </el-form-item>
    <el-form-item :label="$t('确认密码')" prop="confirmPassword">
      <el-input v-model="user.confirmPassword" :placeholder="$t('确认密码')" type="password" show-password auto-complete="new-password" />
    </el-form-item>
    <el-form-item :style="{'text-align':align ? align : ''}">
      <el-button type="primary" :style="{width: width ? width : '' + ';'+customStyle}" :size="size" @click="submit">{{ $t('保存') }}</el-button>
      <!-- <el-button type="danger" size="mini" @click="close">{{ $t('关闭') }}</el-button> -->
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserPwd } from '@/api/system/user'

export default {
  props: {
    size: {
      type: String,
      default: 'mini'
    },
    align: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: ''
    },
    customStyle: {
      type: String,
      default: ''
    },
    isJumpHome: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const password = (rule, value, callback) => {
      var passwordreg = /(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,16}/
      if (!passwordreg.test(value)) {
        callback(new Error(this.$t('密码必须由数字、字母、特殊字符组合,请输入6-16位')))
      } else {
        callback()
      }
    }
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error(this.$t('两次输入的密码不一致')))
      } else {
        callback()
      }
    }
    return {
      test: '1test',
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' },
          { type: 'string', message: this.$t('请输入不包含空格的字符'), trigger: 'blur',
            transform(value) {
              if (value && value.indexOf(' ') === -1) {
                return value
              } else {
                return false
              }
            }
          },
          { required: true, validator: password, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' },
          { required: true, validator: equalToPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(
            response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('修改成功'))
                this.user = {
                  oldPassword: undefined,
                  newPassword: undefined,
                  confirmPassword: undefined
                }
                if (this.isJumpHome) {
                  this.$router.push({ path: '/' })
                }
              } else {
                this.msgError(response.msg)
              }
            }
          )
        } else {
          this.locationError(document)
        }
      })
    },
    close() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.push({ path: '/index' })
    }
  }
}
</script>
